<?php
namespace app\admin;

use think\facade\Db;

class Module
{
    public static function install($moduleName)
    {
        $file = APP_PATH . "{$moduleName}/install.sql";
        if (!file_exists($file)) {
            throw new \Exception("安装脚本不存在: {$file}");
        }

        $sql = file_get_contents($file);
        self::executeSql($sql);
    }

    public static function uninstall($moduleName)
    {
        $file = APP_PATH . "{$moduleName}/uninstall.sql";
        if (!file_exists($file)) {
            throw new \Exception("卸载脚本不存在: {$file}");
        }

        $sql = file_get_contents($file);
        self::executeSql($sql);
    }

    protected static function executeSql($sql)
    {
        $statements = array_filter(array_map('trim', explode(';', $sql)));
        foreach ($statements as $statement) {
            if ($statement) {
                Db::execute($statement);
            }
        }
    }
}
